package com.ruoyi.luban.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.luban.domain.WXUserLoginInfo;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.domain.vo.WxLoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public class WXBaseController extends BaseController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取登录用户
     *
     * @param token
     * @return
     */
    protected WXUserLoginInfo getLoginUserInfo(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        Object o = redisTemplate.opsForValue().get(token);
        if (o == null) {
            return null;
        }
        return JSON.parseObject(o.toString(), WXUserLoginInfo.class);
    }

    /**
     * 获取登录用户
     *
     * @param token
     * @return
     */
    protected LoginUserAndTokenVo getLoginUserInfo2(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        Object o = redisTemplate.opsForValue().get(token);
        if (o == null) {
            return null;
        }
        return JSON.parseObject(o.toString(), LoginUserAndTokenVo.class);
    }


    /**
     * 设置登录用户
     *
     * @param loginUserAndTokenVo
     * @return
     */
    protected void setLoginUserInfo2(LoginUserAndTokenVo loginUserAndTokenVo) {
        logger.info("设置登录用户属性："+ JSONObject.toJSONString(loginUserAndTokenVo));
        redisTemplate.opsForValue().set(loginUserAndTokenVo.getAuthToken(), com.alibaba.fastjson.JSON.toJSONString(loginUserAndTokenVo), 1, TimeUnit.DAYS);
    }

    protected void setRegisters(String openId, LoginUserAndTokenVo loginUserAndTokenVo) {
        logger.info("设置登录参数："+ com.alibaba.fastjson2.JSONObject.toJSONString(loginUserAndTokenVo));
        redisTemplate.opsForValue().setIfAbsent(openId, com.alibaba.fastjson.JSON.toJSONString(loginUserAndTokenVo), 30, TimeUnit.MINUTES);

    }
}
